/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.classes;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.SuperColumn;
import org.cassandra.CassandraAccess;

/**
 *
 * @author marc
 */
public class Conference {
    private java.util.UUID uuid;
    private String name;
    private String url;
    private String planned_date;
    protected ArrayList<Join> array_join=null;
    protected ArrayList<Chat> array_chat = null;
    protected ArrayList<Talk> array_talk = null;

    public static final String ATTRIB_NAME="name";
    public static final String ATTRIB_URL="url";
    public static final String ATTRIB_PLANNED_DATE="planned_date";

    public static final String CFNAME="Conferences";

    public Conference(java.util.UUID uuid,String name, String url, String planned_date) {
        this.uuid = uuid;
        this.name = name;
        this.url = url;
        this.planned_date = planned_date;
    }

    public Conference(String name, String url, String planned_date) {
        this.uuid = CommonUtils.getTimeUUID();
        this.name = name;
        this.url = url;
        this.planned_date = planned_date;
    }

    public Conference() {
        
    }


    public String save(){
        String resp="";

        CassandraAccess ca=new CassandraAccess(CommonUtils.URL_DATABASE, CommonUtils.PORT_DATABASE, CommonUtils.BBBCONFERENCE_KEYSPACE);
        try {
            resp += ca.insertValue(CFNAME, null, this.uuid.toString(), ATTRIB_NAME.getBytes("UTF-8"), this.name);
            resp+=" ";
            resp += ca.insertValue(CFNAME, null, this.uuid.toString(), ATTRIB_URL.getBytes("UTF-8"), this.url);
            resp+=" ";
            resp+=ca.insertValue(CFNAME,null, this.uuid.toString(), ATTRIB_PLANNED_DATE.getBytes("UTF-8"), this.planned_date);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Conference.class.getName()).log(Level.SEVERE, null, ex);
        }
        ca.closePoolCassandra();
        return resp;
    }

    public static ArrayList<Conference> list(){
        ArrayList<Conference> result_list= new ArrayList<Conference>();

        //methods for get a map result: map<key,<columns>>, key=uuid conference
        CassandraAccess ca=new CassandraAccess(CommonUtils.URL_DATABASE, CommonUtils.PORT_DATABASE, CommonUtils.BBBCONFERENCE_KEYSPACE);
        ArrayList<String> namescol=new ArrayList<String>();
        namescol.add(ATTRIB_NAME);
        Map<String,List<Column>> maplist=ca.getListByRange(CFNAME, namescol);

        ArrayList<String> setkeys=new ArrayList(maplist.keySet());
        for(int i=0;i<setkeys.size();i++){
            Conference item_conf=new Conference();
            String keyconf=setkeys.get(i);
            item_conf.setUuid(java.util.UUID.fromString(keyconf));
            List<Column> lc=maplist.get(keyconf);
            for(int j=0;j<lc.size();j++)
            {
                Column colattrib=lc.get(j);
                if(new String(colattrib.getName()).equalsIgnoreCase(ATTRIB_NAME)){
                    item_conf.setName(new String(colattrib.getValue()));
                }
            }
            result_list.add(item_conf);
        }

        ca.closePoolCassandra();
        return result_list;
    }

    public static String getListByUUID(String key){
        //Conference conf=new Conference();
        String resp="";
        CassandraAccess ca=new CassandraAccess(CommonUtils.URL_DATABASE, CommonUtils.PORT_DATABASE, CommonUtils.BBBCONFERENCE_KEYSPACE);
        List<SuperColumn> listsupercolumn=ca.readSuperValues(Activity.CFNAME, key);

        if(listsupercolumn==null)
            return listsupercolumn.size()+"";
        for (int i=0;i<listsupercolumn.size();i++)
        {
            SuperColumn supercolumn=listsupercolumn.get(i);
           List<Column> columnList = supercolumn.getColumns();
           resp=resp+"supercolumn: "+java.util.UUID.nameUUIDFromBytes(supercolumn.getName());
           resp+="\n";
           resp+="====================================";
           resp+="\n\n";
           for (Column column : columnList){
               resp=resp+("column:"+new String(column.getName())+" value:"+ new String(column.getValue()));
               resp+="\n";
               //new String(column.getName())
           }
           resp+="\n";
        }

        ca.closePoolCassandra();

        return resp;
        //return conf;
    }
    /**
     * @return the uuid
     */
    public java.util.UUID getUuid() {
        return uuid;
    }

    /**
     * @param uuid the uuid to set
     */
    public void setUuid(java.util.UUID uuid) {
        this.uuid = uuid;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * @return the planned_date
     */
    public String getPlanned_date() {
        return planned_date;
    }

    /**
     * @param planned_date the planned_date to set
     */
    public void setPlanned_date(String planned_date) {
        this.planned_date = planned_date;
    }

    /**
     * @return the array_join
     */
    protected ArrayList<Join> getArray_join() {
        return array_join;
    }

    /**
     * @param array_join the array_join to set
     */
    protected void setArray_join(ArrayList<Join> array_join) {
        this.array_join = array_join;
    }

    /**
     * @return the array_chat
     */
    protected ArrayList<Chat> getArray_chat() {
        return array_chat;
    }

    /**
     * @param array_chat the array_chat to set
     */
    protected void setArray_chat(ArrayList<Chat> array_chat) {
        this.array_chat = array_chat;
    }

    /**
     * @return the array_talk
     */
    protected ArrayList<Talk> getArray_talk() {
        return array_talk;
    }

    /**
     * @param array_talk the array_talk to set
     */
    protected void setArray_talk(ArrayList<Talk> array_talk) {
        this.array_talk = array_talk;
    }

}
